//register.js
//用户注册页
//author: 徐汉域
var util = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
var wxLogin = require('../../utils/wxlogin.js');
var userService = require('../../services/userService.js');
var app = getApp();
Page({
  data: {
    containerHeight: 0,
    boxHeight: 0,
    loginHeight: 0,
    mobileNum: '',
    verifyCode: '',
    verifyCodeBtnStatus: {
      disabled: false,
      text: '点击获取验证码',
      btnClass: 'verifyBtn',
    },
    timeInterval: 0,
    intervalObj: null,
    referer: 'pages/index/index',
  },
  onLoad: function(option) {
    this.setData({
      referer: decodeURIComponent(option.referer)
    })
    this.setContainerHeight();
  },
  setContainerHeight: function () {
    var that = this;  
    wx.getSystemInfo( {  
      success: function(res) {  
        var boxHeight = Math.floor(res.windowHeight - res.windowWidth / 750 * 30);
        var loginHeight = Math.floor(res.windowHeight - res.windowWidth / 750 * 200);
        that.setData({  
          containerHeight: res.windowHeight,
          boxHeight: boxHeight,
          loginHeight: loginHeight,
        });  
      }
    }); 
  },
  bindMobileInput: function(e) {
    this.setData({
      mobileNum: e.detail.value
    })
  },
  bindVerifyCodeInput: function(e) {
    this.setData({
      verifyCode: e.detail.value
    })
  },
  getVerifyCode: function(e) {
    var that = this;
    userService.sendVerifyCode({
      mobile: this.data.mobileNum,
      success: function (data) {
        var timeInterval = 60;
        var text = timeInterval + '秒后重新发送';

        that.setData({
          timeInterval: timeInterval,
          verifyCodeBtnStatus: {
            disabled: true,
            text: text,
            btnClass: 'verifyBtn-gray'
          }
        });

        var intervalObj = setInterval(that.refreshSmsButton, 1000);

        that.setData({
          intervalObj: intervalObj
        });
      } 

    });
  },
  //提交获取验证码之后，按钮变灰显示XX秒后重新发送
  refreshSmsButton: function () {
    if (this.data.timeInterval == 1) {
      this.setData({
        timeInterval: 0,
        verifyCodeBtnStatus: {
          disabled: false,
          text: '点击获取验证码',
          btnClass: 'verifyBtn'
        }
      });
      clearInterval(this.data.intervalObj);
    } else {
      var timeInterval = this.data.timeInterval - 1;
      var text = timeInterval + '秒后重新发送';
      this.setData({
        timeInterval: timeInterval,
        verifyCodeBtnStatus: {
          disabled: true,
          text: text,
          btnClass: 'verifyBtn-gray'
        }
      });
    }
  },
  bindMobileNum: function(e) {
    var that = this;

    wxLogin.doLogin(app, {
      success: function(data) {
        userService.bindUserWithVerifyCode({
          mobile: that.data.mobileNum,
          openid: data.openid,
          avatar: data.avatar,
          nickname: data.nickname,
          verifyCode: that.data.verifyCode,
          success: function(tokenInfo, userInfo) {
            app.globalData.apiUser = tokenInfo.user_id;
            app.globalData.apiToken = tokenInfo.access_token;
            app.globalData.sysUserInfo = userInfo;
            
            var redirect = '../../' + that.data.referer;
            var res = wx.getSystemInfoSync();
            if (res.SDKVersion) {
              if (res.SDKVersion >= '1.1.0') {
                wx.reLaunch({
                  url: redirect,
                });
              } else {
                wx.switchTab({
                  url: '../../pages/index/index',
                  success: function () {
                    
                    if (redirect == '../../pages/find/find' || redirect == '../../pages/myself/myself') {
                      wx.switchTab({
                        url: redirect,
                      });
                    } else {
                      wx.navigateTo({
                        url: redirect,
                      });
                    }
                    
                  }
                });
              }
            } else {
              wx.switchTab({
                url: '../../pages/index/index',
                success: function () {
                  if (redirect == '../../pages/find/find' || redirect == '../../pages/myself/myself') {
                    wx.switchTab({
                      url: redirect,
                    });
                  } else {
                    wx.navigateTo({
                      url: redirect,
                    });
                  }
                }
              });
            }
          }
        });
      }
    });
  },
  backTopPrevNavigate: function () {
    wx.switchTab({
      url: '../index/index',
    })
  }
})